{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Subscriber with JSON export\n", "\n", "__NOTE__: this is an __outdated__ notebook, some of the functions that are used here are considered __private__ to QCoDeS and are not intended for use by users (for example, `DataSet.subscribe`). This notebook will be re-written in the future." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import copy\n", "import logging\n", "from pathlib import Path\n", "\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from qcodes.dataset import (\n", " ParamSpec,\n", " initialise_or_create_database_at,\n", " load_or_create_experiment,\n", " new_data_set,\n", ")\n", "from qcodes.dataset.json_exporter import (\n", " export_data_as_json_heatmap,\n", " export_data_as_json_linear,\n", " json_template_heatmap,\n", " json_template_linear,\n", ")\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "logging.basicConfig(level=\"INFO\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "initialise_or_create_database_at(Path.cwd() / \"json_export_example.db\")\n", "exp = load_or_create_experiment('json-export-subscriber-test', 'no-sample')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:qcodes.dataset.sqlite.queries:Set the run_timestamp of run_id 109 to 1561115223.856142\n" ] } ], "source": [ "dataSet = new_data_set(\"test\",\n", " exp_id=exp.exp_id,\n", " specs=[ParamSpec(\"x\", \"numeric\"), ParamSpec(\"y\", \"numeric\")])\n", "dataSet.mark_started()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "mystate = {}\n", "mystate['json'] = copy.deepcopy(json_template_linear)\n", "mystate['json']['x']['name'] = 'xname'\n", "mystate['json']['x']['unit'] = 'xunit'\n", "mystate['json']['x']['full_name'] = 'xfullname'\n", "mystate['json']['y']['name'] = 'yname'\n", "mystate['json']['y']['unit'] = 'yunit'\n", "mystate['json']['y']['full_name'] = 'yfullname'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [], "source": [ "sub_id = dataSet.subscribe(export_data_as_json_linear, min_wait=0, min_count=20,\n", " state=mystate, callback_kwargs={'location': 'foo'})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "s = dataSet.subscribers[sub_id]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'json': {'type': 'linear',\n", " 'x': {'data': [],\n", " 'name': 'xname',\n", " 'full_name': 'xfullname',\n", " 'is_setpoint': True,\n", " 'unit': 'xunit'},\n", " 'y': {'data': [],\n", " 'name': 'yname',\n", " 'full_name': 'yfullname',\n", " 'is_setpoint': False,\n", " 'unit': 'yunit'}}}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mystate" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": false }, "outputs": [], "source": [ "for x in range(100):\n", " y = x\n", " dataSet.add_results([{\"x\":x, \"y\":y}])\n", "dataSet.mark_completed()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'json': {'type': 'linear',\n", " 'x': {'data': [0,\n", " 1,\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 7,\n", " 8,\n", " 9,\n", " 10,\n", " 11,\n", " 12,\n", " 13,\n", " 14,\n", " 15,\n", " 16,\n", " 17,\n", " 18,\n", " 19,\n", " 20,\n", " 21,\n", " 22,\n", " 23,\n", " 24,\n", " 25,\n", " 26,\n", " 27,\n", " 28,\n", " 29,\n", " 30,\n", " 31,\n", " 32,\n", " 33,\n", " 34,\n", " 35,\n", " 36,\n", " 37,\n", " 38,\n", " 39,\n", " 40,\n", " 41,\n", " 42,\n", " 43,\n", " 44,\n", " 45,\n", " 46,\n", " 47,\n", " 48,\n", " 49,\n", " 50,\n", " 51,\n", " 52,\n", " 53,\n", " 54,\n", " 55,\n", " 56,\n", " 57,\n", " 58,\n", " 59,\n", " 60,\n", " 61,\n", " 62,\n", " 63,\n", " 64,\n", " 65,\n", " 66,\n", " 67,\n", " 68,\n", " 69,\n", " 70,\n", " 71,\n", " 72,\n", " 73,\n", " 74,\n", " 75,\n", " 76,\n", " 77,\n", " 78,\n", " 79,\n", " 80,\n", " 81,\n", " 82,\n", " 83,\n", " 84,\n", " 85,\n", " 86,\n", " 87,\n", " 88,\n", " 89,\n", " 90,\n", " 91,\n", " 92,\n", " 93,\n", " 94,\n", " 95,\n", " 96,\n", " 97,\n", " 98,\n", " 99],\n", " 'name': 'xname',\n", " 'full_name': 'xfullname',\n", " 'is_setpoint': True,\n", " 'unit': 'xunit'},\n", " 'y': {'data': [0,\n", " 1,\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 7,\n", " 8,\n", " 9,\n", " 10,\n", " 11,\n", " 12,\n", " 13,\n", " 14,\n", " 15,\n", " 16,\n", " 17,\n", " 18,\n", " 19,\n", " 20,\n", " 21,\n", " 22,\n", " 23,\n", " 24,\n", " 25,\n", " 26,\n", " 27,\n", " 28,\n", " 29,\n", " 30,\n", " 31,\n", " 32,\n", " 33,\n", " 34,\n", " 35,\n", " 36,\n", " 37,\n", " 38,\n", " 39,\n", " 40,\n", " 41,\n", " 42,\n", " 43,\n", " 44,\n", " 45,\n", " 46,\n", " 47,\n", " 48,\n", " 49,\n", " 50,\n", " 51,\n", " 52,\n", " 53,\n", " 54,\n", " 55,\n", " 56,\n", " 57,\n", " 58,\n", " 59,\n", " 60,\n", " 61,\n", " 62,\n", " 63,\n", " 64,\n", " 65,\n", " 66,\n", " 67,\n", " 68,\n", " 69,\n", " 70,\n", " 71,\n", " 72,\n", " 73,\n", " 74,\n", " 75,\n", " 76,\n", " 77,\n", " 78,\n", " 79,\n", " 80,\n", " 81,\n", " 82,\n", " 83,\n", " 84,\n", " 85,\n", " 86,\n", " 87,\n", " 88,\n", " 89,\n", " 90,\n", " 91,\n", " 92,\n", " 93,\n", " 94,\n", " 95,\n", " 96,\n", " 97,\n", " 98,\n", " 99],\n", " 'name': 'yname',\n", " 'full_name': 'yfullname',\n", " 'is_setpoint': False,\n", " 'unit': 'yunit'}}}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mystate" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "mystate = {}\n", "xlen = 5\n", "ylen = 10\n", "mystate['json'] = json_template_heatmap.copy()\n", "mystate['data'] = {}\n", "mystate['data']['xlen'] = xlen\n", "mystate['data']['ylen'] = ylen\n", "mystate['data']['x'] = np.zeros((xlen*ylen), dtype=object)\n", "mystate['data']['x'][:] = None\n", "mystate['data']['y'] = np.zeros((xlen*ylen), dtype=object)\n", "mystate['data']['y'][:] = None\n", "mystate['data']['z'] = np.zeros((xlen*ylen), dtype=object)\n", "mystate['data']['z'][:] = None\n", "mystate['data']['location'] = 0" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:qcodes.dataset.sqlite.queries:Set the run_timestamp of run_id 110 to 1561115225.747753\n" ] } ], "source": [ "dataSet_hm = new_data_set(\"test\", exp_id=exp.exp_id,\n", " specs=[ParamSpec(\"x\", \"numeric\"),\n", " ParamSpec(\"y\", \"numeric\"),\n", " ParamSpec(\"z\", \"numeric\")])\n", "dataSet_hm.mark_started()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "sub_id = dataSet_hm.subscribe(export_data_as_json_heatmap, min_wait=0, min_count=20,\n", " state=mystate, callback_kwargs={'location': './foo'})" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": false }, "outputs": [], "source": [ "for x in range(xlen):\n", " for y in range(ylen):\n", " z = x+y\n", " dataSet_hm.add_results([{\"x\":x, \"y\":y, 'z':z}])\n", "dataSet_hm.mark_completed()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'type': 'heatmap',\n", " 'x': {'data': [0, 1, 2, 3, 4],\n", " 'name': '',\n", " 'full_name': '',\n", " 'is_setpoint': True,\n", " 'unit': ''},\n", " 'y': {'data': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " 'name': '',\n", " 'full_name': '',\n", " 'is_setpoint': True,\n", " 'unit': ''},\n", " 'z': {'data': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n", " [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n", " [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],\n", " [4, 4, 4, 4, 4, 4, 4, 4, 4, 4]],\n", " 'name': '',\n", " 'full_name': '',\n", " 'is_setpoint': False,\n", " 'unit': ''}}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mystate['json']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }